package com.easy.stock.spider.html;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import com.easy.stock.dao.StockFeedItemDao;
import com.easy.stock.dao.StockInfoDao;
import com.easy.stock.domain.StockFeedItem;
import com.easy.stock.domain.StockInfo;
import com.easy.stock.spider.BaseAsyncCompletionHandler;
import com.easy.stock.spider.SpiderTask;
import com.easy.stock.spider.SpiderTask.STATUS;
import com.easy.stock.util.Constants;
import com.easy.stock.util.JsoupUtil;
import com.ning.http.client.Response;

public class SATalkSpider extends BaseHtmlSpider {

	@Autowired
	private StockInfoDao stockInfoDao;
	
	@Autowired
	private StockFeedItemDao stockFeedItemDao;
	
	private String baseUrl;
	
	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	
	@Override
	protected List<SpiderTask> prepareSeeds() {
		
		List<SpiderTask> tasks = new ArrayList<SpiderTask>();
		List<StockInfo> stocks = null;
		int limit=1000;
		int offset=0;
		while(!(stocks=stockInfoDao.getStockInfoList(offset, limit)).isEmpty()) {
			for(StockInfo stock : stocks) {
				SpiderTask task = new SpiderTask(baseUrl+stock.getId()+"/stocktalks", new DetailHandler(this, stock.getId()), STATUS.READY);
				tasks.add(task);
			}
			
			offset+=limit;
		}
		return tasks;
		
//		// for test
//		List<SpiderTask> tasks = new ArrayList<SpiderTask>();
//		SpiderTask task = new SpiderTask(baseUrl+"INSY"+"/stocktalks", new DetailHandler(this, "CMGE"), STATUS.READY);
//		tasks.add(task);
//		return tasks;
	}
	
	public static class DetailHandler extends BaseAsyncCompletionHandler {

		private String stockId;
		private SimpleDateFormat sdf = new SimpleDateFormat("MMM d, h:mm aa yyyy", Locale.US);
		private SimpleDateFormat sdf2 = new SimpleDateFormat("EEE, h:mm aa yyyy", Locale.US);

		private String currentYear;

		public DetailHandler(BaseHtmlSpider spider, String stockId) {
			super.spider = spider;
			this.stockId = stockId;
		}
		
		@Override
		public List<SpiderTask> doOnCompleted(Response response)
				throws IOException {
			
			currentYear = new SimpleDateFormat("yyyy").format(new Date());
			SATalkSpider mySpider = (SATalkSpider)spider;
			String content = response.getResponseBody("UTF-8");
			Document doc = Jsoup.parse(content);
			Elements elems = doc.select("#stocktalk_list > ul");
			int elemNum = elems.size();
			for(int i=0; i<elemNum; i++) {
				Element elem = elems.get(i);
				
				Element originElem = elem.select("li.origin").first();
				if(originElem==null)
					continue;
				
				StockFeedItem originItem = parseFeed(originElem);
				if(originItem==null)
					continue;
				
				// 已经存在的不抓取
				if(mySpider.stockFeedItemDao.getFeedItem(originItem.getId())!=null)
					continue;
				
				Elements replyElems = elem.select("li.reply");
				if(replyElems.size()>0) {
					int replyNum = replyElems.size();
					List<StockFeedItem> replyItems = new ArrayList<StockFeedItem>();
					for(int j=0; j<replyNum; j++) {
						StockFeedItem replyItem = parseFeed(replyElems.get(j));
						if(replyItem==null)
							continue;
						replyItems.add(replyItem);
					}
					originItem.setReplies(replyItems);
				}
				mySpider.stockFeedItemDao.addFeedItem(originItem);
			}
			
			LOG.info("finish crawl stock talk page, stockId="+stockId+", talks="+elemNum);
			return null;
		}
		
		private StockFeedItem parseFeed(Element elem) {
			StockFeedItem item = new StockFeedItem();
			item.setAuthor_name(JsoupUtil.getElemText(elem, "div.nick_cnt a.stocktalker_nick", true));
			String dateStr = JsoupUtil.getElemText(elem, "div.time_wrapper span.time_ago", true);
			if(dateStr.indexOf("Second")>0) {
				String minute = dateStr.split(" ", -1)[0];
				item.setTime(new Date(System.currentTimeMillis()-Long.parseLong(minute)*1000));
			} else if(dateStr.indexOf("minute")>0) {
				String minute = dateStr.split(" ", -1)[0];
				item.setTime(new Date(System.currentTimeMillis()-Long.parseLong(minute)*60*1000));
			} else if(dateStr.indexOf("hour")>0) {
				String hour = dateStr.split(" ", -1)[0];
				item.setTime(new Date(System.currentTimeMillis()-Long.parseLong(hour)*60*60*1000));
			} else if(dateStr.indexOf("day")>0) {
				String hour = dateStr.split(" ", -1)[0];
				item.setTime(new Date(System.currentTimeMillis()-Long.parseLong(hour)*24*60*60*1000));
			} else if(dateStr.indexOf("/")>0) {
				// 不处理，跳过
				return null;
			} else {
				try {
					dateStr = dateStr+" "+currentYear;
					if(dateStr.split(",", -1)[0].length()==3)
						item.setTime(sdf2.parse(dateStr));
					else
						item.setTime(sdf.parse(dateStr));
				} catch (ParseException e) {
					throw new RuntimeException("fail to parse stocktalk date, str="+dateStr);
				}
			}
			item.setContent(JsoupUtil.getElemText(elem, "div.content_cnt" , true));
			item.setId(Constants.SEEKING_ALPHA+elem.attr("st_id"));
			item.setSrc(Constants.SEEKING_ALPHA);
			item.setStock_id(stockId);
			return item;
		}
		
		@Override
		public void doOnThrowable(Throwable t) {
			// do nothing
		}
		
	}
}
